package com.dhcc.cdc.busi.controller;


import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.cdc.busi.entity.CdcExpitemEntity;
import com.dhcc.cdc.busi.service.ICdcExpitemService;
import com.dhcc.core.framework.annotion.BussinessLog;
import com.dhcc.core.framework.annotion.Permission;
import com.dhcc.core.framework.base.controller.BaseController;
import com.dhcc.core.framework.base.warpper.EmptyWarpper;
import com.dhcc.core.framework.exception.BizException;
import com.dhcc.core.framework.exception.BizExceptionEnum;
import com.dhcc.core.framework.result.R;
import com.dhcc.core.framework.result.page.PageFactory;
import com.dhcc.core.framework.util.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * 费用项目
 * 
 * @ClassName: CdcExpitemController
 * @Description: TODO
 * @author jumper
 * @date 2021-03-15 10:20:07
 */
@Controller
@RequestMapping("${sys.backendPath}/cdc/cdcexpitem")
public class CdcExpitemController extends BaseController {

	private String PREFIX = "/backend/cdc/cdcexpitem/";

	@Autowired
	private ICdcExpitemService cdcExpitemService;
	

    /**
     * 跳转到费用项目列表首页
     */
    @RequestMapping("")
    @Permission("cdcexpitem:list")
    public String index() {
        return PREFIX + "cdcexpitem.html";
    }
	
    /**
     * 跳转到新增费用项目
     */
    @RequestMapping("/to_add")
    @Permission("cdcexpitem:add")
    public String toAdd() {
        return PREFIX + "cdcexpitem_add.html";
    }
	
	/**
     * 跳转到修改费用项目
     */
    @RequestMapping("/to_update/{cdcExpitemId}")
    @Permission("cdcexpitem:update")
    public String toUpdate(@PathVariable Long cdcExpitemId, Model model) {
        CdcExpitemEntity cdcExpitem = this.cdcExpitemService.selectById(cdcExpitemId);
        model.addAttribute("cdcExpitem",cdcExpitem);
        return PREFIX + "cdcexpitem_edit.html";
    }

    /**
     * 获取费用项目列表
     */
	@SuppressWarnings("unchecked")
    @RequestMapping(value = "/list")
    @ResponseBody
    @Permission("cdcexpitem:list")
    public Object list(@RequestParam Map<String, Object> params) {
		Page<CdcExpitemEntity> page = new PageFactory<CdcExpitemEntity>().defaultPage();

		List<CdcExpitemEntity> result=null;
		if((CommonUtil.isNotEmpty(params.get("NotQuery")))&&params.get("NotQuery").toString().equals("true")){
		    result=new ArrayList<CdcExpitemEntity>();
        }else{
            result = this.cdcExpitemService.page(page,params);
        }
        page.setRecords((List<CdcExpitemEntity>) new EmptyWarpper(result).warp());
        return R.packForBT(page);
    }

    /**
     * 新增费用项目
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    @Permission("cdcexpitem:add")
    @BussinessLog(value = "新增费用项目")
    public Object add(CdcExpitemEntity cdcExpitem) {
        if (CommonUtil.isOneEmpty(cdcExpitem)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        cdcExpitem.insert();
        return R.ok();
    }

    /**
     * 删除费用项目
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    @Permission("cdcexpitem:delete")
    @BussinessLog(value = "删除费用项目")
    public Object delete(@RequestParam Long cdcExpitemId) {
	
        this.cdcExpitemService.deleteById(cdcExpitemId);

        return R.ok();
    }

    /**
     * 修改费用项目
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    @Permission("cdcexpitem:update")
    @BussinessLog(value = "修改费用项目")
    public Object update(CdcExpitemEntity cdcExpitem) {
        if (CommonUtil.isOneEmpty(cdcExpitem)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        CdcExpitemEntity old = this.cdcExpitemService.selectById(cdcExpitem.getId());

        old.setExpenseId(cdcExpitem.getExpenseId());
        old.setCode(cdcExpitem.getCode());
        old.setDescription(cdcExpitem.getDescription());
        old.setCost(cdcExpitem.getCost());
        old.setExpenseDate(cdcExpitem.getExpenseDate());
        old.setIsActive(cdcExpitem.getIsActive());
        old.updateAllColumnById();
        return R.ok();
    }
	
}
